package Dao.TeacherDao;

import Entity.Course;
import Entity.SelectCourse;
import Entity.Student;
import Entity.Teacher;
import Util.SQLParse;
import Util.SQLUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/*
教师后台 Dao层 负责SQL语句 与传入参数的传入
 */
public class TeacherSQLdao {
    private SQLUtil sqlUtil = new SQLUtil();


    /*
    插入一个教师
    需要参数 学生所有信息(出了描述信息)
    返回数据库修改成功个数
     */
    public int insertTeacher(Teacher teacher){
         String teacherId = teacher.getTeacherId();
         String teacherPassword = teacher.getTeacherPassword();
         String teacherName = teacher.getTeacherName();
         String teacherGrade = teacher.getTeacherGrade();
         String teacherType = teacher.getTeacherType();
         String department= teacher.getDepartment();

         Object object[] = new Object[]{teacherId,teacherPassword,teacherName,teacherGrade,teacherType,department};

         String sql = "insert into teacherInfo(teacherId,teacherPassword,teacherName,teacherGrade,teacherType,department) VALUES(?,?,?,?,?,?)";

         return sqlUtil.execute(sql,object);
    }


    /*
    查看id是否存在
    需要参数 教师Id
    返回数据库查询到的个数
     */
    public int queryIdIsExist(String teacherId){
        Object object[] = new Object[]{teacherId};
        String sql = "select count(teacherId) from teacherInfo where teacherId = ? ";
        ResultSet rs = sqlUtil.executeQuery(sql,object);
        return SQLParse.parseCount(rs);
    }


    /*
    教师登录 需要参数 教师id 教师密码
    需要参数 教师Id 教师密码
    返回查询到的教师 0或1个
     */
    public Teacher selectTeacherByTeacherIdAndPassword(Teacher teacher){

        Object object[] = new Object[]{teacher.getTeacherId(),teacher.getTeacherPassword()};

        String sql = "select *from teacherinfo where teacherId = ? and teacherPassword = ?";

        Teacher tea = selectTeacher(sql,object);
        if (tea!=null) return tea;
        return null;
    }




    /*
    登录之后在后台内 不需要密码 可直修改个人信息 通过id直接修改
    需要参数 教师Id
     返回整个教师信息
     */
    public Teacher selectTeacherNoPassword(Teacher teacher){

        Object object[] = new Object[]{teacher.getTeacherId()};

        String sql = "select *from teacherinfo where teacherId = ?";

        Teacher tea = selectTeacher(sql,object);
        if (tea!=null) return tea;
        return null;
    }


    /*
    未登陆状态 通过教师id修改密码
    需要参数 教师账号 教师密码
    返回数据库修改成功个数
     */
    public int modifyPassword(Teacher teacher){

        Object object[] = new Object[]{teacher.getTeacherPassword(),teacher.getTeacherId()};
        String sql = "UPDATE teacherInfo set teacherPassword = ? where teacherId = ?";
        return sqlUtil.execute(sql,object);
    }


    /*
    修改教师信息 成绩与描述
    需要信息学生描述  教师Id  教师成绩
    返回数据库修改成功个数
     */
    public int updateByTeacherId(Teacher teacher){
        String teacherId = teacher.getTeacherId();
        String teacherDesc =teacher.getTeacherDesc();

        Object object[] = new Object[]{teacherDesc,teacherId};

        String sql = "UPDATE teacherinfo SET teacherDesc = ? WHERE teacherId = ?";


        return  sqlUtil.execute(sql,object);
    }


    /*
    教师添加课程
    需要参数 所有课程信息
    返回数据库修改成功个数
     */
    public int insertCourseInfo(Course course){
        String courseId = course.getCourseId();
        String courseName = course.getCourseName();
        String courseType = course.getCourseType();
        float  courseCredit =  course.getCourseCredit();
        int courseHour = course.getCourseHour();
        int courseVolume = course.getCourseVolume();
        String teacherId = course.getTeacherId();
        String courseDesc =course.getCourseDesc();
        int status = course.getStatus();

        Object object[] = new Object[]{courseId,courseName,courseType,courseCredit,courseHour,courseVolume,teacherId,courseDesc,status};

        String sql = "insert into courseinfo(courseId,courseName,courseType,courseCredit,courseHour,courseVolume,teacherId,courseDesc,status) values(?,?,?,?,?,?,?,?,?)";


        return sqlUtil.execute(sql,object);
    }


    /*
     删除教师自己发布的课程
     需要参数 一个课程Id的链表
     返回数据库修改成功个数
     */
    public int deleteTeacherCourse(Course course){
        String teacherId = course.getTeacherId();
        String courseId = course.getCourseId();
        Object object[] = new Object[]{courseId,teacherId};

        String sql = "DELETE FROM courseinfo WHERE courseId = ? and teacherId = ?";

        return sqlUtil.execute(sql,object);
    }


    /*
    教师修改学生成绩
    需要参数 一个教师Id 学生Id 课程Id联合一起的类
    返回数据库修改成功个数
     */
    public int updateStudentScore(SelectCourse selectCourse){
        String studentId = selectCourse.getStudentId();
        String teacherId = selectCourse.getTeacherId();
        String courseId = selectCourse.getCourseId();
        float score = selectCourse.getScore();

        Object object[] = new Object[]{score,studentId,teacherId,courseId};

        String sql = "UPDATE selectCourseInfo SET score = ? WHERE studentId = ? and teacherId = ? and courseId = ?";

        return sqlUtil.execute(sql,object);
    }


    /*
    未登陆状态下 通过教师Id  教师姓名是否存在该人
    需要参数 教师Id 教师姓名
    返回 真假
     */
    public boolean selectTeacherByIdAndName(String teacherId, String teacherName){

        Object object[] = new Object[]{teacherId,teacherName};

        String sql = "select *from teacherInfo where teacherId =? and teacherName = ?";

        ResultSet rs = sqlUtil.executeQuery(sql,object);

        try {
            if (rs.next()){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }


    /*
    未登录状态下 修改登陆密码
    需要参数 教师Id 教师姓名 教师密码
    返回数据库修改成功个数
     */
    public int updatePasswordNoLogin(String teacherId,String teacherName, String teacherPassword){
        Object object[] = new Object[]{teacherPassword,teacherId,teacherName};
        String sql = "update teacherInfo set teacherPassword = ? where teacherId =? and teacherName = ?";
        return sqlUtil.execute(sql,object);
    }






    /*
   辅助性公共方法 帮助解析ResultSet解析出Teacher对象
   需要参数 sql语句 对应的参数
   返回单个教师的信息
    */
    public Teacher selectTeacher( String sql, Object[] object){


        ResultSet rs = sqlUtil.executeQuery(sql,object);

        Teacher tea = null;

        try {
            if (rs.next()) {
                String teacherId =  rs.getString("teacherId");
                String teacherPassword = rs.getString("teacherPassword");
                String teacherName =  rs.getString("teacherName");
                String teacherGrade =  rs.getString("teacherGrade");
                String teacherType =  rs.getString("teacherType");
                String department =  rs.getString("department");
                String teacherDesc =  rs.getString("teacherDesc");

                tea = new Teacher();
                tea.setTeacherId(teacherId);
                tea.setTeacherPassword(teacherPassword);
                tea.setTeacherName(teacherName);
                tea.setTeacherGrade(teacherGrade);
                tea.setTeacherType(teacherType);
                tea.setDepartment(department);
                tea.setTeacherDesc(teacherDesc);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }

        return tea;
    }








}
